﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace JapaneseMusicHtmlCreator
{
    /// <summary>
    /// PageWelcome.xaml 的交互逻辑
    /// </summary>
    public partial class PageWelcome : Page
    {
        private MainWindow mainWindow;
        private BackgroundWorker worker;
        private Complete complete;
        public PageWelcome(MainWindow mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
            tbFileComplete.Visibility = Visibility.Hidden;
           
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (mainWindow.savedSettingsPath == null)
            {
                complete = Complete.None;
                LoadSettings();
                mainWindow.NavigateForward(this, mainWindow.pageInformation);
            }
            else
            {
                tbFileComplete.Visibility = Visibility.Visible;
                btnNext.IsEnabled = false;
                worker = new BackgroundWorker();
                worker.DoWork += worker_DoWork;
                worker.RunWorkerCompleted += worker_RunWorkerCompleted;
                worker.RunWorkerAsync();
            }
            
        }

      

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnNext.IsEnabled = true;
            tbFileComplete.Visibility = Visibility.Hidden;
            LoadSettings();
            mainWindow.NavigateForward(this, mainWindow.pageInformation);
            
        }

        public void LoadSettings()
        {
            mainWindow.pageInformation.tbMusicArtist.Text = mainWindow.musicArtist;
            mainWindow.pageInformation.tbMusicPath.Text = mainWindow.musicPath;
            mainWindow.pageInformation.tbMusicTitle.Text = mainWindow.musicTitle;
            mainWindow.pageKashiEdit.tbMusicArtist.Text = mainWindow.musicArtist;
            mainWindow.pageKashiEdit.tbKashi.Text = mainWindow.musicKashi;
            mainWindow.pageKashiEdit.tbMusicTitle.Text = mainWindow.musicTitle;
            mainWindow.pageSettings.btnKashiColor.Background = new SolidColorBrush(mainWindow.musicKashiColor) ;
            mainWindow.pageSettings.btnCurrentKashiColor.Background = new SolidColorBrush(mainWindow.musicCurrentKashiColor);
            mainWindow.pageSettings.tbAnimeTime.Text = mainWindow.musicLineAnimeTime.ToString();
            mainWindow.pageSettings.btnKanaColor.Background = new SolidColorBrush(mainWindow.musicKanaColor);
            mainWindow.pageSettings.btnTranslateColor.Background = new SolidColorBrush(mainWindow.musicTranslateColor);
            mainWindow.pageSettings.btnTitleColor.Background = new SolidColorBrush(mainWindow.musicTitleColor);
            mainWindow.pageSettings.btnArtistColor.Background = new SolidColorBrush(mainWindow.musicArtistColor);
            mainWindow.pageSettings.btnBackgroundColor.Background = new SolidColorBrush(mainWindow.musicBackgroundColor);
            mainWindow.pageSettings.tbCurrentLineFontSize.Text= mainWindow.musicCurrentLineFontSize.ToString();
            mainWindow.pageSettings.tbLineFontSize.Text= mainWindow.musicLineFontSize.ToString();
            mainWindow.pageSettings.tbTitleFontSize.Text = mainWindow.musicTitleFontSize.ToString();
            mainWindow.pageSettings.tbArtistFontSize.Text = mainWindow.musicArtistFontSize.ToString();
            mainWindow.pageSettings.sliderOpa.Value = mainWindow.musicBkgndValue;
            if (mainWindow.musicBkgndUseBlack)
            {
                mainWindow.pageSettings.rbBlack.IsChecked = true;
            }
            else
            {
                mainWindow.pageSettings.rbWhite.IsChecked = true;
            }
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(200);
            complete = CheckComplete(mainWindow.savedSettingsPath,worker);
            if (complete == Complete.Incomplete)
            {
                System.Windows.MessageBox.Show("损坏的jmh文件，将使用新的配置打开", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                ResetJmh();
            }
        }

        private void ResetJmh()
        {
            mainWindow.musicTitle = null;//100
            mainWindow.musicArtist = null;//100
            mainWindow.musicPath = null;//1000
            mainWindow.musicBkgndPath = "delete";//1000
            mainWindow.musicBkgndPath2 = "delete";//1000
            mainWindow.musicKashi = null;//10000
            mainWindow.musicBkgndValue = 0;//8
            mainWindow.musicBkgndUseBlack = false;//1
            mainWindow.musicKashiColor = Colors.Black;//32
            mainWindow.musicTranslateColor = Colors.Black;//32
            mainWindow.musicKanaColor = Colors.Black;//32
            mainWindow.musicCurrentKashiColor = Colors.Black;//32
            mainWindow.musicTitleColor = Colors.Black;//32
            mainWindow.musicArtistColor = Colors.Black;//32
            mainWindow.musicBackgroundColor = Colors.White;//32
            mainWindow.musicLineFontSize = 0;//8
            mainWindow.musicCurrentLineFontSize = 0;//8
            mainWindow.musicLineAnimeTime = 0;//8
            mainWindow.musicTitleFontSize = 0;//8
            mainWindow.musicArtistFontSize = 0;//8
        }

        private Complete CheckComplete(string path, BackgroundWorker worker)
        {
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    using (BinaryReader reader = new BinaryReader(fs))
                    {
                        char[] temp=new char[100];
                        temp=reader.ReadChars(100);
                        mainWindow.musicTitle = GetString(temp);
                        temp = reader.ReadChars(100);
                        mainWindow.musicArtist = GetString(temp);
                        temp = new char[1000];
                        temp = reader.ReadChars(1000);
                        mainWindow.musicPath = GetString(temp);
                        temp = reader.ReadChars(1000);
                        mainWindow.musicBkgndPath = GetString(temp);
                        temp = reader.ReadChars(1000);
                        mainWindow.musicBkgndPath2 = GetString(temp);
                        temp = new char[10000];
                        temp = reader.ReadChars(10000);
                        mainWindow.musicKashi = GetString(temp);
                        mainWindow.musicBkgndValue = reader.ReadDouble();
                        mainWindow.musicBkgndUseBlack = reader.ReadBoolean();
                        mainWindow.musicKashiColor = ReadColor(reader);
                        mainWindow.musicTranslateColor = ReadColor(reader);
                        mainWindow.musicKanaColor = ReadColor(reader);
                        mainWindow.musicCurrentKashiColor = ReadColor(reader);
                        mainWindow.musicTitleColor = ReadColor(reader);
                        mainWindow.musicArtistColor = ReadColor(reader);
                        mainWindow.musicBackgroundColor = ReadColor(reader);
                        mainWindow.musicLineFontSize = reader.ReadDouble();
                        mainWindow.musicCurrentLineFontSize = reader.ReadDouble();
                        mainWindow.musicLineAnimeTime = reader.ReadDouble();
                        mainWindow.musicTitleFontSize = reader.ReadDouble();
                        mainWindow.musicArtistFontSize = reader.ReadDouble();
                        
                    }
                }

            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                return Complete.Incomplete;
            }
            return Complete.Complete;
        }

        private Color ReadColor(BinaryReader reader)
        {
            char[] ch=reader.ReadChars(10);

            return Color.FromArgb(GetHex(ch[2], ch[3]), GetHex(ch[4], ch[5]), GetHex(ch[6], ch[7]), GetHex(ch[8], ch[9]));
        }

        private byte GetHex(char p1,char p2)
        {
            return (byte)(HexToByte(p1) * 16 + HexToByte(p2));
        }
        private byte HexToByte(char p)
        {
            if (p >= '0' && p <= '9') return (byte)(p - '0');
            else if (p >= 'A' && p <= 'F') return (byte)(p - 'A' + 10);
            return 255;
        }

        private string GetString(char[] temp)
        {
            string ret = "";
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i] != '\0')
                    ret += temp[i];
                else return ret;
            }
            return ret;
        }

      

        private void tbLoadSavedSettings_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.CurrentDirectory;
            ofd.Filter = "jmh 文件|*.jmh";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(ofd.FileName))
                {
                    tbLoadSavedSettings.Content = ofd.SafeFileName;
                    mainWindow.savedSettingsPath = ofd.FileName;
                }
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
